#############################################################
## Replication file for Ban, Palmer and Schneer 2019       ##
## file: rdd_plots_with_cis.R                              ##
## date: 5/16/2019                                         ##
#############################################################

#install.packages(c('tidyverse','haven','janitor','glue'))

#Load Packages
library(tidyverse)
library(haven)
library(janitor)
library(glue)
options(stringsAsFactors = FALSE)

#set working directory
setwd("/nfs/home/B/bschneer/shared_space/bschneer/lobbying_replication")


#load data from previously generated files
h.data <- read_dta("usr_gen/candidates_H_for_rdd_plots.dta") %>% data.frame()
s.data <- read_dta("usr_gen/candidates_S_for_rdd_plots.dta") %>% data.frame()

bin_width = .2/20
# Bring in the data

rdd.vars <- data.frame(outcome=c("became_lobbyist", 
                                 "amount_per_lobbyist_yrly", 
                                 "amount_weighted_lim_yrly", 
                                 "amount_per_lobbyist", 
                                 "amount_weighted_lim", 
                                 "rainmaker", 
                                 "ln_amount_weighted_lim", 
                                 "ln_amount_per_lobbyist"),
                       outcome.label=c("Became a Lobbyist", 
                                       "Lobbyist Revenues per Person per Year ($k)", 
                                       "Lobbyist Weighted Revenues per Year  ($k)", 
                                       "Lobbyist Revenues per Person  ($k)", 
                                       "Lobbyist Weighted Revenues  ($k)", 
                                       "Rainmakers", 
                                       "log(LVA Weighted Revenues)", 
                                       "log(Amount Per Lobbyist)"))

h.data <- mutate_at(h.data, vars("amount_per_lobbyist_yrly", 
                                 "amount_weighted_lim_yrly", 
                                 "amount_per_lobbyist", 
                                 "amount_weighted_lim"), list(~ ./1000))
s.data <- mutate_at(s.data, vars("amount_per_lobbyist_yrly", 
                                 "amount_weighted_lim_yrly", 
                                 "amount_per_lobbyist", 
                                 "amount_weighted_lim"), list(~ ./1000))

#Create plot function
rdd.plot <- function(data, x, y, z) {
    g <- data %>% mutate(treat=as.integer(vote_margin>0)) %>%
        filter(abs(vote_margin)<=.2) %>%
        select(treat, rv=vote_margin, outcome=x) %>%
        filter(!is.na(outcome)) %>%
        group_by(treat) %>%
        mutate(bin=floor(rv/bin_width)*bin_width) %>%
        group_by(bin) %>%
        mutate(bin_outcome = mean(outcome), bin_rv = mean(rv)) %>%
        ggplot() +
        geom_vline(xintercept=0, col="gray30", alpha=1, size=1, lty="dashed") +
        geom_point(aes(x=bin_rv, y=bin_outcome), col="gray20", 
                   size=1, alpha=1, pch=21, fill="white") +
        stat_smooth(aes(x=rv, y=outcome, group=treat), 
                    method="lm", fill="gray40", color="black") +
        xlab("Vote Margin") +
        ylab(y) +
        xlim(-.2, .2) + coord_cartesian() +
        theme_minimal()
    ggsave(file=glue("out/rd_{x}_{z}.pdf"), g, width=6, height=4, units="in")
}

pwalk(rdd.vars, ~ rdd.plot(h.data, .x, .y, "House"))
pwalk(rdd.vars, ~ rdd.plot(s.data, .x, .y, "Senate"))

